/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package storymotion.keyword;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import storymotion.dao.DAO;

/**
 *
 * @author Leonardo Oliveira Moreira
 */
public class KeywordDAO extends DAO {

    public KeywordDAO() throws SQLException {
    }

    public void insert(Keyword keyword) throws SQLException {
        if (keyword == null || keyword.getPhotoId() == null || keyword.getKeyword() == null || keyword.getKeyword().trim().length() == 0) {
            throw new SQLException("Could not perform this action");
        }
        try {
            PreparedStatement preparedStatement = null;
            preparedStatement = connection.prepareStatement("INSERT INTO keywords (photo_id, keyword) VALUES (?, ?)");
            preparedStatement.setLong(1, keyword.getPhotoId());
            preparedStatement.setString(2, keyword.getKeyword());
            int result = preparedStatement.executeUpdate();
            if (result != 1) {
                throw new SQLException("Could not perform this action");
            }
            preparedStatement.close();
        } catch (SQLException ex) {
            throw ex;
        }
    }
    
    public void deleteByPhotoId(long photoId) throws SQLException {
        if (photoId < 0) {
            throw new SQLException("Could not perform this action");
        }
        try {
            PreparedStatement preparedStatement = null;
            preparedStatement = connection.prepareStatement("DELETE FROM keywords WHERE photo_id = ?");
            preparedStatement.setLong(1, photoId);
            int result = preparedStatement.executeUpdate();
            if (result < 1) {
                throw new SQLException("Could not perform this action");
            }
            preparedStatement.close();
        } catch (SQLException ex) {
            throw ex;
        }
    }

    public List<Keyword> getByPhotoId(long photoId) throws SQLException {
        if (photoId < 1) {
            throw new SQLException("Could not perform this action");
        }
        List<Keyword> result = new ArrayList<Keyword>();
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("SELECT photo_id, keyword FROM keywords WHERE photo_id = ?");
            preparedStatement.setLong(1, photoId);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Keyword keyword = new Keyword();
                keyword.setPhotoId(resultSet.getLong("photo_id"));
                keyword.setKeyword(resultSet.getString("keyword"));
                result.add(keyword);
            }
            resultSet.close();
            preparedStatement.close();
        } catch (SQLException ex) {
            throw ex;
        }
        return result;
    }
}
